home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / prs2 / prs2inout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  6.9 KB  |  322 lines

  1. /*======================================================================
  2.  *
  3.  * FILE:
  4.  * prs2inout.c
  5.  *
  6.  * IDENTIFICATION:
  7.  * $Header: /private/postgres/src/rules/prs2/RCS/prs2inout.c,v 1.7 1991/06/18 15:40:44 sp Exp $
  8.  *
  9.  * These are the in/out routines for rule locks.
  10.  *
  11.  *======================================================================
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <strings.h>
  16. #include "rules/prs2.h"
  17. #include "utils/log.h"
  18. #include "utils/palloc.h"
  19.  
  20. /*-------------------------------------------------------------
  21.  * these routines are local to this file.
  22.  *-------------------------------------------------------------
  23.  */
  24. static char *appendString();
  25. static void skipToken();
  26. static long readLongInt();
  27. static char readChar();
  28.  
  29. #define ISBLANK(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
  30.  
  31.  
  32. /*--------------------------------------------------------------------
  33.  *
  34.  * RuleLockToString
  35.  *
  36.  * Given a rule lock create a string containing a represantation
  37.  * of a rule lock suitable for reading by human beings
  38.  *
  39.  * NOTE: space is allocated for the string. When you don't need it
  40.  * anymore, use 'pfree()'
  41.  */
  42. char *
  43. RuleLockToString(locks)
  44. RuleLock locks;
  45. {
  46.     char *res, *temp;
  47.     char s1[200];
  48.     int i;
  49.     int maxLength;
  50.     Prs2OneLock oneLock;
  51.  
  52.  
  53.     /*
  54.      * Initialize `maxLength' and `res'
  55.      */
  56.     maxLength = 100;
  57.     res = (char *) palloc(maxLength);
  58.     if (res==NULL) {
  59.     elog(WARN, "RuleLockToString: Out of memeory!");
  60.     }
  61.     res[0] = '\0';
  62.  
  63.     /*
  64.      * empty locks are a special case...
  65.      */
  66.     if (locks == NULL) {
  67.     /*
  68.      * a NULL lock is assumed to be an empty lock
  69.      */
  70.     res = appendString(res, "(numOfLocks: 0)", &maxLength);
  71.     return(res);
  72.     }
  73.  
  74.     /*
  75.      * Deal with a non empty lock...
  76.      */
  77.     sprintf(s1, "(numOfLocks: %d", locks->numberOfLocks);
  78.     res = appendString(res, s1, &maxLength);
  79.  
  80.     for (i=0; i<prs2GetNumberOfLocks(locks); i++) {
  81.     /*
  82.      * NOTE: oid are long ints, plan numbers are uint16, i.e. shorts
  83.      * ditto for attribute numbers,  and lock types are chars.
  84.      */
  85.     oneLock = prs2GetOneLockFromLocks(locks, i);
  86.     sprintf(s1,
  87.     " (ruleId: %ld lockType: %c attrNo: %d planNo: %d partindx: %d npart: %d)",
  88.         prs2OneLockGetRuleId(oneLock),
  89.         prs2OneLockGetLockType(oneLock),
  90.         prs2OneLockGetAttributeNumber(oneLock),
  91.         prs2OneLockGetPlanNumber(oneLock),
  92.         prs2OneLockGetPartialIndx(oneLock),
  93.         prs2OneLockGetNPartial(oneLock));
  94.     res = appendString(res, s1, &maxLength);
  95.     }
  96.  
  97.     res = appendString(res, ")", &maxLength);
  98.  
  99.     return(res);
  100. }
  101.  
  102. /*--------------------------------------------------------------------
  103.  *
  104.  * StringToRuleLock
  105.  *
  106.  * this is the opposite of 'RuleLockToString'.
  107.  * Given a string representing a rule lock, recreate the original
  108.  * rulelock.
  109.  *
  110.  * NOTE: space is allocated for the new lock. Pfree it when 
  111.  * you don't need it any more...
  112.  *
  113.  */
  114. RuleLock
  115. StringToRuleLock(s)
  116. char *s;
  117. {
  118.     int i, numOfLocks;
  119.     ObjectId ruleId;
  120.     Prs2LockType lockType;
  121.     AttributeNumber attrNo;
  122.     Prs2PlanNumber planNo;
  123.     int partialindx, npartial;
  124.     int index;
  125.     long l;
  126.     char c;
  127.     RuleLock lock;
  128.  
  129.  
  130.     /*
  131.      * Create an initially empty rule lock.
  132.      */
  133.     lock =  prs2MakeLocks();
  134.  
  135.     /*
  136.      * Now read the number of locks
  137.      */
  138.     index = 0;
  139.     skipToken("(", s, &index);
  140.     skipToken("numOfLocks:", s, &index);
  141.     l = readLongInt(s, &index);
  142.     numOfLocks = (int) l;
  143.  
  144.     for (i=0; i<numOfLocks; i++) {
  145.     /*
  146.      * read each lock's data
  147.      */
  148.     skipToken("(", s, &index);
  149.     skipToken("ruleId:", s, &index);
  150.     l = readLongInt(s, &index);
  151.     ruleId = (ObjectId) l;
  152.     skipToken("lockType:", s, &index);
  153.     c = readChar(s, &index);
  154.     lockType = (Prs2LockType) c;
  155.     skipToken("attrNo:", s, &index);
  156.     l = readLongInt(s, &index);
  157.     attrNo = (AttributeNumber) l;
  158.     skipToken("planNo:", s, &index);
  159.     l = readLongInt(s, &index);
  160.     planNo = (Prs2PlanNumber) l;
  161.     skipToken("partindx:", s, &index);
  162.     l = readLongInt(s, &index);
  163.     partialindx = (Prs2PlanNumber) l;
  164.     skipToken("npart:", s, &index);
  165.     l = readLongInt(s, &index);
  166.     npartial = (Prs2PlanNumber) l;
  167.     skipToken(")", s, &index);
  168.     /*
  169.      * now append them into the rule lock structure..
  170.      */
  171.     lock = prs2AddLock(lock, ruleId, lockType, attrNo, planNo,
  172.                 partialindx, npartial);
  173.     }
  174.  
  175.     skipToken(")", s, &index);
  176.     return(lock);
  177. }
  178.  
  179. /*====================================================================
  180.  * UTILITY ROUTINES LOCAL TO THIS FILE
  181.  *====================================================================
  182.  */
  183.  
  184. /*--------------------------------------------------------------------
  185.  *
  186.  * appendString
  187.  *
  188.  * append string 's' to the string 'res'. *maxLengthP is the
  189.  * memory size allocated for 'res'. If this is not enough to hold
  190.  * both the old contents of 'res' and the appended string 's',
  191.  * allocate some more memory. 
  192.  * It returns the result of the string concatenation, and
  193.  * if more space has been allocated, *maxLengthP is updated.
  194.  */
  195.  
  196. static char*
  197. appendString(res, s, maxLengthP)
  198. char *res;
  199. char *s;
  200. int *maxLengthP;
  201. {
  202.     char *temp;
  203.  
  204.     if (strlen(s) + strlen(res) >= *maxLengthP -1) {
  205.     while (strlen(s) + strlen(res) >= *maxLengthP -1) {
  206.         *maxLengthP += 100;
  207.     }
  208.     temp = (char *) palloc(*maxLengthP);
  209.     if (temp==NULL) {
  210.         elog(WARN, "appendString: Out of memory!");
  211.     }
  212.     strcpy(temp, res);
  213.     pfree(res);
  214.     res = temp;
  215.     }
  216.     strcat(res, s);
  217.     return(res);
  218. }
  219.  
  220. /*--------------------------------------------------------------------
  221.  *
  222.  * skipToken
  223.  *
  224.  * read & skip the specified token 'token' from the string that
  225.  * starts at 's[*index]'. '*index' is incremented accordingly
  226.  * If there is a mismatch, signal a syntax error.
  227.  */
  228. static void
  229. skipToken(token, s, index)
  230. char *token;
  231. char *s;
  232. int *index;
  233. {
  234.     char *t;
  235.  
  236.     /*
  237.      * skip blanks
  238.      */
  239.     while (ISBLANK(s[*index])) {
  240.     (*index)++;
  241.     }
  242.  
  243.     t = token;
  244.     while (*t != '\0') {
  245.     if (s[*index] != *t) {
  246.         elog(WARN,"Syntax error while reading RuleLock, token=%s", token);
  247.     }
  248.     t++;
  249.     (*index)++;
  250.     }
  251. }
  252.  
  253. /*--------------------------------------------------------------------
  254.  *
  255.  * readLongInt
  256.  *
  257.  * read an (long) integer from the string that
  258.  * starts at 's[*index]'. '*index' is incremented accordingly
  259.  */
  260. static long
  261. readLongInt(s, index)
  262. char *s;
  263. int *index;
  264. {
  265.     long res;
  266.     int sign;
  267.  
  268.     /*
  269.      * skip blanks
  270.      */
  271.     while (ISBLANK(s[*index])) {
  272.     (*index)++;
  273.     }
  274.  
  275.     /*
  276.      * Is this a negative number ?
  277.      */
  278.     sign = 1;
  279.     if (s[*index] == '-') {
  280.     sign = -1;
  281.     (*index)++;
  282.     }
  283.  
  284.     /*
  285.      * Now read the number
  286.      */
  287.     res = 0;
  288.     while (s[*index] <='9' && s[*index] >= '0') {
  289.     res = 10*res + s[*index] - '0';
  290.     (*index)++;
  291.     }
  292.  
  293.     res = res * sign;
  294.     return(res);
  295. }
  296.  
  297. /*--------------------------------------------------------------------
  298.  *
  299.  * readChar
  300.  *
  301.  * read the first non blank character from the string that
  302.  * starts at 's[*index]'. '*index' is incremented accordingly
  303.  */
  304. static char
  305. readChar(s, index)
  306. char *s;
  307. int *index;
  308. {
  309.     char c;
  310.  
  311.     /*
  312.      * skip blanks
  313.      */
  314.     while (ISBLANK(s[*index])) {
  315.     (*index)++;
  316.     }
  317.  
  318.     c = s[*index];
  319.     (*index)++;
  320.     return(c);
  321. }
  322.